package com.exam.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import com.exam.dao.StudentDao;
import com.exam.model.StudentModel;
import com.exam.service.PageService;
import com.exam.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class StudentAction extends ActionSupport implements ModelDriven<StudentModel>,
Preparable,RequestAware {
	private HttpServletResponse response = ServletActionContext.getResponse();
	private PrintWriter out = null;
	private String message;
	
	private static final long serialVersionUID = 805890851165708981L;
	private StudentModel student;
	private String pageNow="1";
	private String sid="";
	private String sname="";
	
	private String gname;  //分组名称
	private int gid; //分组id
	
	public String getGname() {
		return gname;
	}
	public void setGname(String gname) {
		this.gname = gname;
	}
	public int getGid() {
		return gid;
	}
	public void setGid(int gid) {
		this.gid = gid;
	}

	/**
	 * 后台管理学生列表
	 * @return
	 */
	public String list(){
		StudentDao sdao = new StudentDao();
		try {
			PageService page = new PageService(sdao.getCount(sid, sname), "student-list.action?sid="+sid+"&sname="+sname+"&", pageNow);
			this.request.put("StudentList", sdao.getStudentList(sid, sname, page.getLimit()));
			this.request.put("pageInfo", page.getPageInfo());
			this.request.put("pageList", page.getPageList());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list-success";
	}
	
	
	/**
	 * 保存考生信息
	 * @return
	 */
	public String save(){
		
		if(this.validateStudent(this.student)){
			System.out.println(student.toString());
			try{
				 out = this.response.getWriter();
				 if(new StudentDao().addOneStudent(student)){
					 out.print("success");
				 }else{
					 out.print("error");
				 }
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public void prepareSave(){
		student = new StudentModel();
	}
	
	
	/*
	 * 编辑 获取回显数据
	 */
	public String edit(){
		return "edit";
	}
	
	public void prepareEdit(){
		 try {
			student = new StudentDao().getOneStudent(sid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新数据
	 * @return
	 */
	public String update(){
		if(this.validateStudent(student)){
		if(new StudentDao().updateStudent(student)){
			return "update-success";
		}else{
			message = "保存失败！";
			return "update-error";
		}
		}else{
			message = "请按要求填写数据!";
			return "update-error";
		}
	}
	
	public void prepareUpdate(){
		student = new StudentModel();
	}
	
	/**
	 * 删除考生信息
	 * @return
	 */
	public String delete(){
		new StudentDao().deleteStudent(sid);
		return "delete";
	}
	
	/*
	 * 验证student
	 */
	private boolean validateStudent(StudentModel stu){
		boolean b = true;
		if(stu.getStu_id().length()>50 || stu.getStu_id().length()<2){
			b = false;
		}
		if(!Tools.isNumeric(student.getStu_id())){
			b = false;
		}
		if(stu.getStu_name().length()>20 || stu.getStu_name().length()<2){
			b = false;
		}
		return b;
	}
	
	
	/*-------------分组 ------------*/
	
	public String listGroup(){
		StudentDao sdao = new StudentDao();
		PageService page;
		try {
			page = new PageService(sdao.getGroupCount(), "student-listGroup.action?", pageNow);
			this.request.put("GroupList", sdao.getAllGroup(page.getLimit()));
			this.request.put("pageInfo", page.getPageInfo());
			this.request.put("pageList", page.getPageList());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list-group";
	}
	
	
	/**
	 * 新增分组
	 */
	public String addGroup(){
		try {
			if(new StudentDao().addGroup(gname)){
				out = response.getWriter();
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//编辑 分组 --获取数据
	public String editGroup(){
		try {
			this.request.put("editGroup", new StudentDao().getOneGroup(gid));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "edit-group";
	}
	//提交修改
	public String updateGroup(){
		StudentDao sd = new StudentDao();
		System.out.println(gname+"=="+gid);
		try {
			if(!sd.isExistGroup(gname)){
				if(sd.updateGroup(gid, gname)){
					out = response.getWriter();
					out.print("success");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
    //删除
	public String deleteGroup(){
		new StudentDao().deleteGroup(gid);
		return "delete-group";
	}
	
	Map<String, Object> request;
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	@Override
	public void prepare() throws Exception {
		//不执行此方法
	}
	@Override
	public StudentModel getModel() {
		return this.student;
	}
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getPageNow() {
		return pageNow;
	}

	public void setPageNow(String pageNow) {
		this.pageNow = pageNow;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
}
